package com.hxzy.dao.impl;

import com.hxzy.bean.User;
import com.hxzy.dao.UserDao;
import com.hxzy.util.JdbcUtils;

import java.util.HashSet;
import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public int save(User user) {
        String sql = "INSERT INTO message_users (nick_name,account,login_password,state)  values (?,?,?,?)";
        Object[] params ={
                user.getNickName(),
                user.getAccount(),
                user.getPassword(),
                user.getState()
        };
        return JdbcUtils.executeUpdate(sql,params);
    }

    @Override
    public int update(User user) {
        String sql = "UPDATE message_users set nick_name=?,account=?,login_password=?,state=? where id=?";
        Object[] params ={
                user.getNickName(),
                user.getAccount(),
                user.getPassword(),
                user.getState(),
                user.getState(),
                user.getId()
        };
        return JdbcUtils.executeUpdate(sql,params);
    }

    @Override
    public int remove(Integer integer) {
        String sql = "DELETE FROM message_users where id=?";
        return JdbcUtils.executeUpdate(sql,integer);
    }

    @Override
    public List<User> queryAll() {
        String sql = new StringBuilder()
                .append("SELECT")
                .append("m.id as 'id',")
                .append("m.nick_name as 'nickName',")
                .append("m.account as 'account',")
                .append("m.login_password as 'password',")
                .append("m.state as 'state'")
                .append("FROM message_users m")
                .toString();
        System.out.println(sql);
        return JdbcUtils.selectSingleTab(User.class,sql);
    }

    @Override
    public User findById(Integer integer) {
        String sql = new StringBuilder()
                .append("SELET")
                .append("m.id as 'id'")
                .append("m.nick_name as 'nickName',")
                .append("m.account as 'account',")
                .append("m.login_password as 'password',")
                .append("m.state as 'state'")
                .append("FROM message_users m")
                .append("WHERE m.id = ?")
                .toString();
        List<User> users = JdbcUtils.selectSingleTab(User.class,sql,integer);
        return users.size() > 0 ? users.get(0): null;
    }

    @Override
    public User findByAccount(String account) {
        String sql = new StringBuilder()
                .append("SELLECT")
                .append("m.id as 'id'")
                .append("m.nick_name as 'nickName',")
                .append("m.account as 'account',")
                .append("m.login_password as 'password',")
                .append("m.state as 'state'")
                .append("FROM message_users m")
                .append("WHEER m.account = ?")
                .toString();
        List<User> users = JdbcUtils.selectSingleTab(User.class,sql,account);
        return users.size() > 0 ? users.get(0) : null;
    }
}
